﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using FileClient.Wpf.Controls.Pager;
using FileCommon.Models;
using FileEfCore.DbContexts;
using Microsoft.Extensions.DependencyInjection;
using Xceed.Wpf.Toolkit;

namespace FileClient.Wpf.ViewModels;

public class CollectionFileItem
{
    public string LocalFileName { get; set; }

    public string ServerFileName { get; set; }

    public OperationType OperationType { get; set; }

    public DateTimeOffset OperationDateTime { get; set; }

    public double ProgressRate { get; set; }

    public long FileLength { get; set; }

    public ProgressStatus ProgressStatus { get; set; }
}

public class CollectionFileGridDataViewModel : NotifyProperty
{
    private Pager<CollectionFileItem> _pager;


    public Pager<CollectionFileItem> Pager
    {
        get => _pager;
        set => SetProperty(ref _pager, value);
    }

    private ObservableCollection<CollectionFileItem> _fileHistoryCollectionPaging;

    public ObservableCollection<CollectionFileItem> FileHistoryCollectionPaging
    {
        get => _fileHistoryCollectionPaging;
        set => SetProperty(ref _fileHistoryCollectionPaging, value);
    }

    public ICommand QueryHistoryFileCommand { get; set; }

    private void ExecuteQueryHistoryFileCommand(object obj)
    {
        var currentPage = Pager.UpdateSource(QueryDataList());
        FileHistoryCollectionPaging = new ObservableCollection<CollectionFileItem>(currentPage);
    }

    private readonly DateTimePicker _startDateTimePicker;
    private readonly DateTimePicker _endDateTimePicker;
    private readonly TextBox _textBoxFileName;

    public CollectionFileGridDataViewModel(DateTimePicker startDateTimePicker, DateTimePicker endDateTimePicker,
        TextBox textBoxFileName)
    {
        _startDateTimePicker = startDateTimePicker;
        _endDateTimePicker = endDateTimePicker;
        _textBoxFileName = textBoxFileName;
        QueryHistoryFileCommand = new RelayCommand(ExecuteQueryHistoryFileCommand);
        Pager = new Pager<CollectionFileItem>(10, QueryDataList());
        Pager.PagerUpdated += items =>
        {
            FileHistoryCollectionPaging = new ObservableCollection<CollectionFileItem>(items);
        };
        Pager.CurPageIndex = 1;
    }

    private List<CollectionFileItem> QueryDataList()
    {
        using var dbContext = App.ServiceProvider.GetRequiredService<FileDbContext>();
        
        var queryResult = dbContext
                .UploadFiles
                .WhereIf(_startDateTimePicker.Value.HasValue,
                    p => p.OperationDateTime >= _startDateTimePicker.Value.Value)
                .WhereIf(_endDateTimePicker.Value.HasValue, p => p.OperationDateTime <= _endDateTimePicker.Value.Value)
                .WhereIf(!string.IsNullOrEmpty(_textBoxFileName.Text),
                    p => p.LocalFileName.Contains(_textBoxFileName.Text) ||
                         p.ServerFileName.Contains(_textBoxFileName.Text))
                .OrderByDescending(p=> p.OperationDateTime)
                .ToList()
            ;
        return queryResult.Select(p => new CollectionFileItem()
        {
            OperationType = p.OperationType,
            OperationDateTime = p.OperationDateTime,
            ProgressRate = p.ProgressRate,
            ProgressStatus = p.ProgressStatus,
            LocalFileName = p.LocalFileName,
            ServerFileName = p.ServerFileName,
            FileLength = p.FileLength
        }).ToList();
    }
}